<template>
  <div class="login-page">
    <el-card class="box-card">
      <!-- <div slot="header" class="clearfix">
        <span>卡片名称</span>
      </div> -->
      <!-- v-slot:header 可以简写成 #header -->
      <template #header>黑马面经运营后台</template>
      <!-- 下面是卡片的内容区，放表单 -->
      <el-form
        autocomplete="off"
        label-width="60px"
        :model="user"
        :rules="rules"
        ref="from"
      >
        <el-form-item prop="username" label="账号">
          <el-input v-model="user.username" placeholder="输入用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password" label="密码">
          <el-input
            v-model="user.password"
            type="password"
            placeholder="输入用户密码"
          ></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 按需导入 API 方法
import { loginAPI } from '@/api/user'

export default {
  name: 'LoginIndex',
  data () {
    return {
      // 数据项 (和表单双向绑定)
      user: {
        username: 'admin',
        password: 'admin'
      },
      // 验证对象
      rules: {
        username: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.from.validate(async valid => {
        // console.log(valid) // 验证通过valid=true；验证不通过valid=false；
        if (valid) {
          // 说明验证通过
          // 把数据 user 提交给接口
          // console.log(this.user)
          const res = await loginAPI(this.user)
          // console.log(res)
          localStorage.setItem('mj-pc-token', res.data.token)
          this.$message.success('登陆成功')
          this.$router.push('/dashboard') // 跳转到图表页
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
// sass 和 stylus 语法上差不多，都要求去掉花括号和分号
// .login-page
//   background-color: pink
//   p
//     color: blue

// scss 和 less 语法上差不多
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
